package com.common.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;

/**
 * Created by Administrator on 2015/7/22.
 */
public class TimeUtils {
    /**
     * 获取小时分钟
     *
     * @return
     */
    public static String getHourMinute(String time) {
        int time1 = Integer.parseInt(time);
        String sh = (time1 / 60) + "";
        String sm = (time1 % 60) + "";
        sh = (sh.length() < 2) ? "0" + sh : sh;
        sm = (sm.length() < 2) ? "0" + sm : sm;

        return sh + ":" + sm;

    }
    public static String getTimeLongMD(long date) {
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
        Date mDate = null;
        String ymd;
        try {
//            long time = Long.parseLong(date);
            long time = date;
            mDate = new Date(time);
            ymd = format.format(mDate);
        } catch (Exception e) {
            mDate = new Date();
            ymd = format.format(mDate);
        }
        return ymd;
    }

    /**
     * 时间转换时：分：秒
     */
    public static String stringForTime(long timeS) {

        StringBuilder formatBuilder = new StringBuilder();
        Formatter formatter = new Formatter(formatBuilder, Locale.getDefault());

        try {
            int totalSeconds = (int) timeS;
            int seconds = totalSeconds % 60;
            int minutes = (totalSeconds / 60) % 60;
            int hours = totalSeconds / 3600;

            formatBuilder.setLength(0);

            return formatter.format("%02d:%02d:%02d", hours, minutes, seconds).toString();
        } finally {
            formatter.close();
        }
    }

    /**
     * 时间转换时：分
     */
    public static String stringForTimeSF(long timeS) {

        StringBuilder formatBuilder = new StringBuilder();
        Formatter formatter = new Formatter(formatBuilder, Locale.getDefault());

        try {
            int totalSeconds = (int) timeS;
            int seconds = totalSeconds % 60;
            int minutes = (totalSeconds / 60) % 60;
            int hours = totalSeconds / 3600;

            formatBuilder.setLength(0);
            if(minutes>0){
                if(hours >= 10){
                    if(minutes>=10){
                        return formatter.format("开播%02d小时%02d分钟", hours, minutes).toString();
                    }else{
                        return formatter.format("开播%02d小时%d分钟", hours, minutes).toString();
                    }
                }else{
                    if(minutes>=10){
                        if(hours>=1){
                            return formatter.format("开播%d小时%02d分钟", hours, minutes).toString();
                        }else{
                            return formatter.format("开播%2d分钟",minutes).toString();
                        }
                    }else if(hours >= 1){
                        return formatter.format("开播%d小时%d分钟", hours, minutes).toString();
                    }else {
                        return formatter.format("开播%d分钟",minutes).toString();
                    }
                }
            }else {
                if(hours >= 10){
                    return formatter.format("开播%02d小时", hours).toString();
                }else if(hours>=1){
                    return formatter.format("开播%d小时", hours).toString();
                }else {
                    return formatter.format("刚刚开播").toString();
                }
            }
        } finally {
            formatter.close();
        }
    }



    /**
     * 时间转换分：秒
     */
    public static String stringForTimeNoHour(long timeMs) {

        StringBuilder formatBuilder = new StringBuilder();
        Formatter formatter = new Formatter(formatBuilder, Locale.getDefault());

        try {
            int totalSeconds = (int) (timeMs / 1000);

            int seconds = totalSeconds % 60;
            int minutes = totalSeconds / 60;

            formatBuilder.setLength(0);

            return formatter.format("%02d:%02d", minutes, seconds).toString();
        } finally {
            formatter.close();
        }
    }
    // 格式：年－月－日 小时：分钟：秒
    public static final String FORMAT_ONE = "yyyy-MM-dd";

    /**
     * 把符合日期格式的字符串转换为日期类型
     *
     * @param dateStr
     * @return
     */
    public static Date stringtoDate(String dateStr, String format) {
        Date d = new Date(dateStr);
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            formater.setLenient(false);
            d = formater.parse(dateStr);
        } catch (Exception e) {
            d = new Date(System.currentTimeMillis());
        }
        return d;
    }
    public static String getTimeLongYMD(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// ���ø�ʽ
        Date mDate = null;
        String ymd;
        try {
            long time = Long.parseLong(date);
            mDate = new Date(time);
            ymd = format.format(mDate);
        } catch (Exception e) {
            mDate = new Date();
            ymd = format.format(mDate);
        }
        return ymd;
    }

    public static long getTimeLongNotYear(String date) {
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");

        Date mDate;
        try {
            mDate = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            mDate = new Date();
        }

        return mDate.getTime();
    }


    public static long getTimeLong(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");// ���ø�ʽ

        Date mDate;
        try {
            mDate = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            mDate = new Date();
        }

        return mDate.getTime();
    }

    /**
     * 获取现在时间
     *
     * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
     */
    public static String getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getNowHMDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
    public static Date getDateByString(String time) {
        Date date = null;
        if(time == null) return date;
        String date_format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat format = new SimpleDateFormat(date_format);
        try{
            date = format.parse(time);
        } catch(ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String getDateByFormatString(String mDate_format){
        String dateString=null;
        Date currentTime = new Date();
        SimpleDateFormat format = new SimpleDateFormat(mDate_format);
        dateString = format.format(currentTime);
        return dateString;
    }

    /** * 获取指定日期是星期几
     * 参数为null时表示获取当前日期是星期几
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        String[] weekOfDays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        Calendar calendar = Calendar.getInstance();
        if(date != null){
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0){
            w = 0;
        }
        return weekOfDays[w];
    }

    public static String getShortTime(String time) {
        String shortstring = null;
        long now = Calendar.getInstance().getTimeInMillis();
        Date date = getDateByString(time);
        if(date == null) return shortstring;
        long deltime = (now - date.getTime())/1000;
        if(deltime > 365*24*60*60) {
            shortstring = (int)(deltime/(365*24*60*60)) + "年前";
        } else if(deltime > 24*60*60) {
            shortstring = (int)(deltime/(24*60*60)) + "天前";
        } else if(deltime > 60*60) {
            shortstring = (int)(deltime/(60*60)) + "小时前";
        } else if(deltime > 60) {
            shortstring = (int)(deltime/(60)) + "分前";
        } else if(deltime > 1) {
            shortstring = deltime + "秒前";
        } else{
            shortstring = "1秒前";
        }
        return shortstring;
    }


    public static String seccondToHour(long second){
        if(second<=0){
            return "过期";
        }
        if(second > 30000000){  //当值大于30000000时，将近于10年时，判断为无截止日期
            return "无截止日期";
        }
        long hour = second / 3600;
        int day ;
        if(hour>24){
            day = (int)hour / 24;
            return day+"天"+(hour%24)+"小时";
        }
        if(hour == 0){
            long minute = second / 60;
            if(minute == 0){
                return "过期";
            }
            return minute +"分钟";
        }
        return hour+"小时";
    }

    /**
     * 检查日期是否不合法
     * @param year
     * @param month
     * @param day
     * @param hour
     * @param minute
     * @return
     */
    public static boolean isDateError(int year,int month,int day,int hour,int minute){
        Calendar calendar= Calendar.getInstance();
        if(year<calendar.get(Calendar.YEAR)){
            return false;
        }else if(year>calendar.get(Calendar.YEAR)){
            return true;
        }
        if(month<calendar.get(Calendar.MONTH)){
            return false;
        }else if(month>calendar.get(Calendar.MONTH)){
            return true;
        }
        if(day<calendar.get(Calendar.DAY_OF_MONTH)){
            return false;
        }else if(day>calendar.get(Calendar.DAY_OF_MONTH)){
            return true;
        }
        if(hour<calendar.get(Calendar.HOUR_OF_DAY)){
            return false;
        }else if(hour>calendar.get(Calendar.HOUR_OF_DAY)){
            return true;
        }
        if(minute<=calendar.get(Calendar.MINUTE)){
            return false;
        }else if(minute>calendar.get(Calendar.MINUTE)){
            return true;
        }
        return true;
    }
}
